//
//  BaseNavigationController.m
//  FlashFishRec
//
//  Created by zhangcong on 2017/9/4.
//  Copyright © 2017年 zhangcong. All rights reserved.
//

#import "BaseNavigationController.h"
#import <objc/runtime.h>
#import <WSConst.h>

@interface BaseNavigationController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>
// 记录push标志
@property (nonatomic, getter=isPushing) BOOL pushing;


@end

@implementation BaseNavigationController

- (id)initWithRootViewController:(UIViewController *)rootViewController
{
    if (self = [super initWithRootViewController:rootViewController]) {
        self.view.backgroundColor = [UIColor clearColor];
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
//        [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:18                    ], NSFontAttributeName,nil]];
        [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kUIColorWithRGB(0x333333), NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:17                    ], NSFontAttributeName,nil]];

//        self.navigationBar.barTintColor = [UIColor whiteColor];
        self.navigationBar.barTintColor = UIColor.blackColor;
        
//            self.titleAttribute =@{NSForegroundColorAttributeName:[UIColor GP_navTitle_color],NSFontAttributeName:[UIFont HelveticaNeueBoldFontSize:18]};
//            [self.navigationController.navigationBar setTitleTextAttributes: self.titleAttribute];
        //    [self.navigationController.navigationBar setTitleTextAttributes: self.titleAttribute];

        self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
        self.navigationController.navigationBar.translucent = NO;
        self.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    return self;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    __weak BaseNavigationController *weakSelf = self;
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.interactivePopGestureRecognizer.delegate = weakSelf;   //自己来响应返回手势
    }
    
    
    
    if (@available(iOS 11.0, *)) {

    } else {
        // Fallback on earlier versions
    }

    
//    [self customNavigationbarBottomColorMethods2];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {

//    if (self.pushing ) {
//        NSLog(@"第一层被拦截");
//        return;
//    }
//    else
    if ([self.topViewController isKindOfClass:[viewController class]]) {
        NSArray * denyArray = @[@"GameRoomWaitViewController",@"ShopStoreDetailVC"];
        NSLog(@"第二层被拦截");
        BOOL deny = NO;
        for (NSString * vcStr in denyArray) {
            if ([viewController isKindOfClass:NSClassFromString(vcStr)]) {
                deny = YES;
                break;
            }
        }
        if(deny)return;
    }
//    else
//    {
//        NSLog(@"push");
//        self.pushing = YES;
//    }
//
    
    
    if (self.viewControllers.count > 0) {
        if ([viewController isKindOfClass:[UIViewController class]]) {
            [viewController setHidesBottomBarWhenPushed:YES];
        }
    }

//    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
//        self.interactivePopGestureRecognizer.enabled = NO;
    
    [super pushViewController:viewController animated:animated];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.topViewController.forbidPanBack) {
        return NO;
    }

//    MainViewController * rootvc = (MainViewController *)MessageUnReadHelper.shared.rootvc;
//
    BOOL canChange = YES;
    BOOL ok = YES;
    if ([gestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
        if (self.viewControllers.count == 1 ) {
            ok = NO;
        }
        if (self.viewControllers.count == 2 && !canChange) {
            ok = NO;
        }
    }
    return ok;
}

//重写父类的手势事件
- (void)handlePanGestureRecognizer:(UIPanGestureRecognizer *)pan {
    if (self.topViewController.forbidPanBack) {
        return;
    }
    
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    [self setNavigationStyle:viewController];
}

-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.pushing = NO;

    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
        self.interactivePopGestureRecognizer.enabled = YES;
}

- (void)setNavigationStyle:(UIViewController *)viewController {
//    UIColor * textColor = [UIColor whiteColor];
    UIColor * textColor = kUIColorWithRGB(0x333333);
    UIColor * navColor =  UIColor.whiteColor;

    [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:textColor, NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:18             ], NSFontAttributeName,nil]];
    self.navigationBar.barTintColor = navColor;
    viewController.navigationController.navigationBar.translucent = NO;
}


- (UIViewController *)childViewControllerForStatusBarStyle {
    
    return self.topViewController;
}

- (UIViewController *)childViewControllerForStatusBarHidden {
    return self.topViewController;
}

-(BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
    
}

//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];;
}


@end
